<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use app\api\controller\Jwt as Jwts;
use app\api\model\Role as RoleModel;

/**
 * @title 宴会预定
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Scheduled extends Api
{
    /**
     * @title 获取宴会厅的类型与预留信息
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/standard_type
     * @method POST
     * @param name:bid type:int require:1 default: other: desc:宴会厅id
     * @return type:类型@
     * @type id:id name:名称 is_meet:1会议2宴会 value:值@
     * @value id:id name:名称 names:值
     */
    public function standard_type(){
        $user = $this->user;
        // 需预定id
        $bid = input('bid');
        if(!input('bid')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        // 宴会厅
        $where[] = ['id','=',$bid];
        $where[] = ['hid','=',$user['hid']];
        // 宴会厅下的类型与餐标
        $banquet = db::name('banquet')->where($where)->field('tid,did')->find();
        $tid = explode( ",",$banquet['tid']);
        $tidc = count($tid);
        
        for ($i=0; $i <$tidc ; $i++) { 
            $types = db::name('banquet_types')->where('id',$tid[$i])->where('is_del',1)->field('id,name,is_meet')->find();
            if($types){
                $type[] = $types;
            }
        }
        foreach ($type as $k => $v) {
            $wheres[] = ['tid','=',$v['id']];
            $wheres[] = ['hid','=',$user['hid']];
            $type[$k]['value'] = db::name('banquet_name')->where($wheres)->field('id,name,names')->select();
            $wheres = [];
        }
        $data['type'] = $type;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 获取宴会厅的餐标
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/standard
     * @method POST
     * @param name:bid type:int require:1 default: other: desc:宴会厅id
     * @return data:餐标@
     * @data id:id name:名称
     * @return type:类型@
     * @type id:id name:名称 is_meet:1会议2宴会 value:值@
     * @value id:id name:名称 names:值
     */
    public function standard(){
        $user = $this->user;
        // 需预定id
        $bid = input('bid');
        if(!input('bid')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        // 宴会厅
        $where[] = ['id','=',$bid];
        $where[] = ['hid','=',$user['hid']];
        // 宴会厅下的类型与餐标
        $banquet = db::name('banquet')->where($where)->field('tid,did')->find();
        $did = explode( ",",$banquet['did']);
        $didc = count($did);

        for ($i=0; $i <$didc; $i++) { 
            $ds = db::name('ds')->where('id',$did[$i])->where('is_del',1)->field('id,name,money,did')->find();
            if($ds){
                $datas[] = $ds;
            }
        }
        $data['data'] = $datas;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 选择增值服务
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/service
     * @method POST
     * 
     * @return list:列表@
     * @list id:服务id name:服务名称
     */
    public function service()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['is_del','=','1'];
        $where[] = ['hid','=',$user['hid']];
        $service = db::name('increase')->where($where)->field('id,name,addtime')->select();
        $data['list'] = $service;
        return $this->ajaxReturn(0,'成功',$data);    
    }
     /**
     * @title 客户来源
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/source
     * @method POST
     * 
     * @return list:列表@
     * @list id:id name:名称
     * 
     */
    public function source()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['type','=','1'];
        $where[] = ['is_del','=','1'];

        $where[] = ['hid','=',$user['hid']];
        $source = db::name('source')->where($where)->field('id,name')->select();
        $data['list'] = $source;
        return $this->ajaxReturn(0,'成功',$data);    
    }
    /**
     * @title 查看客户经理
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/view
     * @method POST
     * 
     */
    public function view()
    {
        $user = $this->user;
        $manager = db::name('user')->where('hid',$user['hid'])->field('id,name')->select();
        $data['list'] = $manager;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 已选菜品查看菜单
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/menu
     * @method POST
     * @param name:did type:int require:1 default: other: desc:菜品id
     * @return list:列表@
     * @list name:名称 data:菜品@
     * @data name:菜名 num:数量 money:金额
     * 
     */
    public function menu()
    {
        $user = $this->user;
        $did = input('did');
        $did = explode(',',$did);
        $registrations = array_count_values($did);
        foreach($registrations as $ks =>&$vs){
            $dish = db::name('dish')->where('id',$ks)->field('name,tid,money')->find();
            $newArr[$ks]['id'] = $ks;
            $newArr[$ks]['name'] = $dish['name'];
            $newArr[$ks]['num'] = $vs;
            $newArr[$ks]['money'] = $dish['money'];
            $newArr[$ks]['tid'] = $dish['tid'];
        }
        $newarray = array();
        foreach($newArr as $a)
        {
            $newarray[] = $a;
        }

        $newarray = $this->group_same_key($newarray,'tid');
        $newarray = array_splice($newarray,0);
        foreach($newarray as $k=>$v){
            $arr[$k]['name'] = db::name('dish_types')->where('id',$v[0]['tid'])->value('name');
            $arr[$k]['data'] = $v;
        }
        $data['list'] = $arr;
        return $this->ajaxReturn(0,'成功',$data);   
    }
    public function group_same_key($arr,$key)
    {
        $new_arr = array();
         foreach($arr as $k=>$v ){
            $new_arr[$v[$key]][] = $v;
        }
        return $new_arr;
    }
    /**
     * @title 获取菜品
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/dish
     * @method POST
     * @param name:name type:string require:0 default: other: desc:搜索菜品名称
     * @param name:tid type:string require:0 default: other: desc:菜品分类id
     * @param name:page type:string require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:菜品id name:菜品名称 money:金额
     */
    public function dish()
    {
        $user = $this->user;
        // 搜索菜品名称
        $name = input('name');
        // 菜品分类
        $tid = input('tid');
        $page = input('page')??1;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['hid','=',$user['hid']];
        if($tid){
            $where[] = ['tid','=',$tid];
            $list = db::name('dish')->where($where)->field('id,name,money,addtime')->order('addtime desc')->page("$page,10")->select();
            $total = db::name('dish')->where($where)->count();
        }elseif($name){
            $where[] = ['name','like',"%".$name."%"];
            $list = db::name('dish')->where($where)->field('id,name,money,addtime')->order('addtime desc')->page("$page,10")->select();
            $total = db::name('dish')->where($where)->count();
        }
        if(!$tid && !$name){
            $wheres[] = ['hid','=',$user['hid']];
            $wheres[] = ['is_hidden','=',1];
            $list = db::name('ds')->where($wheres)->field('id,name,did,money')->select();
            foreach ($list as $k => $v) {
                if($v['did']){
                    $newArr = array();
                    $did = explode( ",",$v['did']);
                    $registrations = array_count_values($did);
                    foreach($registrations as $ks =>&$vs){
                        $newArr[$ks]['id'] = $ks;
                        $newArr[$ks]['num'] = $vs;
                    }
                    $newarray = array();
                    foreach($newArr as $a)
                    {
                        $newarray[] = $a;
                    }
                    $list[$k]['did'] = $newarray;
                }
            }
        }
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);    
    }
    /**
     * @title 获取菜品分类
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/dish_type
     * @method POST
     * 
     * @return dish_type:菜品分类@
     * @dish_type id:分类id name:分类名称
     */
    public function dish_type()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['hid','=',$user['hid']];
        $dish_type = db::name('dish_types')->where($where)->field('id,name')->select();
        $data['dish_type'] = $dish_type;
        return $this->ajaxReturn(0,'成功',$data);   
    }

    /**
     * @title 预收款的类型与方式
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/payment
     * @method POST
     * 
     * @return pay:支付方式@
     * @pay id:id name:名称
     * 
     * @return type:押金类型@
     * @type id:id name:名称
     */
    public function payment()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=','1'];

        $where[] = ['type','=',1];
        $type = db::name('deposit')->where($where)->field('id,name')->select();
        $wheres[] = ['is_hidden','=','1'];
        $wheres[] = ['is_del','=','1'];

        $wheres[] = ['hid','=',$user['hid']];
        $wheres[] = ['type','=',2];
        $pay = db::name('deposit')->where($wheres)->field('id,name')->select();
        $data['pay'] = $pay;
        $data['type'] = $type;
        return $this->ajaxReturn(0,'成功',$data);    
    }
    /**
     * @title 查看已选增值服务
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/service_set
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * @return data:列表@
     * @data b_name:宴会厅 desk:桌数 prepare_desk:备桌 book:预订人 begin:宴会时间 director:客户经理 director_mobile:客户经理电话
     * @return service:列表@
     * @service name:名称 num:数量 money:金额
     * 
     * @return count:增值服务总计
     * 
     */
    public function service_set()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $datas = db::name('scheduled')->where('id',$id)
        ->field('b_name,desk,prepare_desk,book,begin_year,begin_month,begin_day,begin_night,director,director_id')->find();
        $datas['director_mobile'] = db::name('user')->where('id',$datas['director_id'])->value('mobile');
        if($datas['begin_night'] == 1){
            $begin_night = '中午';
        }else{
            $begin_night = '晚上';
        }
        $datas['begin'] = $datas['begin_year'] . '-' .$datas['begin_month'] . '-' .$datas['begin_day'] . ' ' .$begin_night;
        unset($datas['director_id']);
        unset($datas['begin_year']);
        unset($datas['begin_month']);
        unset($datas['begin_day']);
        unset($datas['begin_night']);

        $where[] = ['did','=',$id];
        $where[] = ['hid','=',$user['hid']];
        $service = db::name('increase_order')->where($where)->field('money,name,num')->select();
        $count = db::name('increase_order')->where($where)->sum('money');

        $data['data'] = $datas;
        $data['service'] = $service;
        $data['count'] = $count;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 查看已选菜品
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/dish_set
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * 
     * @return data:列表@
     * @data b_name:宴会厅 desk:桌数 prepare_desk:备桌 book:预订人 begin:宴会时间 director:客户经理 director_mobile:客户经理电话 d_money:套餐金额 
     * @return newArrays:列表@
     * @newArrays name:名称 datas:菜品@
     * @datas name:名称 num:数量 money:金额
     * 
     * @return count:增值服务总计
     * 
     */
    public function dish_set()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $datas = db::name('scheduled')->where('id',$id)
        ->field('b_name,desk,prepare_desk,book,begin_year,begin_month,begin_day,begin_night,director,director_id,d_money,cid')->find();
        $datas['director_mobile'] = db::name('user')->where('id',$datas['director_id'])->value('mobile');
        if($datas['begin_night'] == 1){
            $begin_night = '中午';
        }else{
            $begin_night = '晚上';
        }
        $datas['begin'] = $datas['begin_year'] . '-' .$datas['begin_month'] . '-' .$datas['begin_day'] . ' ' .$begin_night;
        unset($datas['director_id']);
        unset($datas['begin_year']);
        unset($datas['begin_month']);
        unset($datas['begin_day']);
        unset($datas['begin_night']);
        if($datas['cid']){
            $dish = explode(',',$datas['cid']);
            $registrations = array_count_values($dish);
            foreach($registrations as $ks =>&$vs){
                $dish = db::name('dish')->where('id',$ks)->field('name,tid,money')->find();
                $newArr[$ks]['id'] = $ks;
                $newArr[$ks]['name'] = $dish['name'];
                $newArr[$ks]['num'] = $vs;
                $newArr[$ks]['money'] = $dish['money'] * $vs;
                $newArr[$ks]['tid'] = $dish['tid'];
            }
            $newarray = array();
            foreach($newArr as $a)
            {
                $newarray[] = $a;
            }
    
            $newarray = $this->group_same_key($newarray,'tid');
            $newarray = array_splice($newarray,0);
            foreach($newarray as $k=>$v){
                $arr[$k]['name'] = db::name('dish_types')->where('id',$v[0]['tid'])->value('name');
                $arr[$k]['datas'] = $v;
            }
        }else{
            $arr = [];
        }
        
        $data['data'] = $datas;
        $data['newArrays'] = $arr;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 选择担保人
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/guarantee
     * @method POST
     * 
     * @return guarantee:列表@
     * @guarantee id:id  name:姓名
     * 
     */
    public function guarantee()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=',1];
        $where[] = ['hid','=',$user['hid']];
        $guarantee = db::name('guarantee')->where($where)->field('id,name')->select();
        $data['guarantee'] = $guarantee;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 修改订单获取数据
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/order_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * @param name:is_del type:int require:0 default: other: desc:潜客信息中的is_del
     * @return data:列表@!
     * @data id:订单id bid:宴会厅id tid:分类id b_name:宴会厅 t_name:类型名 desk:桌数 prepare_desk:备桌 d_name:餐标名 book:预订人 sex:1男2女 mobile:预订人手机号 did:餐标id increase_money:增值服务金额 increase_id:增值服务id director:客户经理 director_id:客户经理id source:客户来源 source_id:客户来源id addtime:预定时间 content:其他说明 is_meeting:1会议2不是会议 begin_time:开始日期 begin_night:1中午2晚上 end_time:结束日期 end_night:1中午2晚上  other:其他信息@ contract:上传合同
     * @contract 
     * @other
     * 
     * @return scheduled_deposit:确认方式@
     * @scheduled_deposit type:1预收款2担保人 status:1未审核2已审核 g_name:担保人姓名 num:预收款编号 image_id:图片id image:预收款图片 money:预收款金额 pay_name:支付方式 type_name:类型
     * 
     *  @return role:权限@!
     *  @role is_say:查看1有0无  is_modify:修改1有0无 is_check:结单1有0无
     * 
     *  @return newArr:新郎新娘等预定信息@
     *  @newArr title:标题 names:姓名等信息 mobile:标题 mobiles:手机号信息等 status:1未审核2已审核
     * 
     * 
     *  
     */
    public function order_del()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $datas = db::name('scheduled')->where('id',$id)
        ->field('id,b_name,bid,tid,t_name,desk,prepare_desk,book,end_year,end_month,end_day,end_night,
        begin_year,begin_month,begin_day,begin_night,d_money,mobile,sex,did,d_name,
        source,director,increase_money,director_id,content,contract,other,is_meeting,cid,addtime,type')->find();
       
        $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
        $pids = 0;
        foreach ($pid as $vs) {
            if($vs['pid'] == 0){
                $pids = 1;
                break;
            }else{
                $pids = 2;
            }
        }
        if($pids == 1){
            if($datas['type'] == 1){
                $is_del = input('is_del');
                if($is_del == 1){
                    $role['is_say'] = 0;
                    $role['is_modify'] = 1;
                    $role['is_check'] = 0;
                }elseif($is_del == 2){
                    $role['is_say'] = 0;
                    $role['is_modify'] = 1;
                    $role['is_check'] = 1;
                }else{
                    $role['is_say'] = 0;
                    $role['is_modify'] = 1;
                    $role['is_check'] = 0;
                }

              
            }elseif($datas['type'] == 2){
                if($datas['is_meeting'] == 1){
                    $banquet_end = $datas['end_year'] . '-' .$datas['end_month'] . '-' . $datas['end_day'];
                    if($datas['end_night'] == 1){
                        $banquet_end = strtotime($banquet_end . "+16 hours");
                    }else{
                        $banquet_end = strtotime($banquet_end . "+23 hours");
                    }
                }else{
                    $banquet_end = $datas['begin_year'] . '-' .$datas['begin_month'] . '-' . $datas['begin_day'];
                    if($datas['begin_night'] == 1){
                        $banquet_end = strtotime($banquet_end . "+16 hours");
                    }else{
                        $banquet_end = strtotime($banquet_end . "+23 hours");
                    }
                }
                if($banquet_end){
                    if(time() < $banquet_end){
                        $role['is_say'] = 0;
                        $role['is_modify'] = 1;
                        $role['is_check'] = 0;
                    }else{
                        $deposit = db::name('scheduled_deposit')->where('sid',$id)->where('status',1)->select();
                        $guarantee = db::name('guarantee_order')->where('did',$id)->where('status',1)->select();
                        $newOrder = array_merge($deposit,$guarantee);
                        $orderCount = count($newOrder);
                        if($orderCount){
                            $role['is_modify'] = 1;
                            $role['is_check'] = 0;
                            
                        }else{
                            $role['is_modify'] = 0;
                            $role['is_check'] = 1;
                            
                        }
                        $role['is_say'] = 0;
                    }
                }
              
            }elseif($datas['type'] == 3){
                $role['is_say'] = 1;
                $role['is_modify'] = 0;
                $role['is_check'] = 0;
            }

        }elseif($pids == 2){
            if($datas['director_id'] == $user['id']){
                if($datas['type'] == 1){
                    $role['is_say'] = 0;
                    $role['is_modify'] = 1;
                    $role['is_check'] = 0;
                }elseif($datas['type'] == 2){
                    if($datas['is_meeting'] == 1){
                        $banquet_end = $datas['end_year'] . '-' .$datas['end_month'] . '-' . $datas['end_day'];
                        if($datas['end_night'] == 1){
                            $banquet_end = strtotime($banquet_end . "+16 hours");
                        }else{
                            $banquet_end = strtotime($banquet_end . "+23 hours");
                        }
                    }else{
                        $banquet_end = $datas['begin_year'] . '-' .$datas['begin_month'] . '-' . $datas['begin_day'];
                        if($datas['begin_night'] == 1){
                            $banquet_end = strtotime($banquet_end . "+16 hours");
                        }else{
                            $banquet_end = strtotime($banquet_end . "+23 hours");
                        }
                    }
                    if($banquet_end){
                        if(time() < $banquet_end){
                            $role['is_check'] = 0;
                            $role['is_say'] = 0;
                            $role['is_modify'] = 1;
                        }else{
                            $deposit = db::name('scheduled_deposit')->where('sid',$id)->where('status',1)->select();
                            $guarantee = db::name('guarantee_order')->where('did',$id)->where('status',1)->select();
                            $newOrder = array_merge($deposit,$guarantee);
                            $orderCount = count($newOrder);
                            if($orderCount){
                                $role['is_check'] = 0;
                                $role['is_modify'] = 1;
                            }else{
                                $role['is_check'] = 1;
                                $role['is_modify'] = 0;
                            }
                            $role['is_say'] = 0;
                        }
                    }
                  
                }elseif($datas['type'] == 3){
                    $role['is_say'] = 1;
                    $role['is_modify'] = 0;
                    $role['is_check'] = 0;
                }
            }else{
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                foreach ($res as $k => $v) {
                    $str1 =str_replace("[","",$res[$k]);
                    $str1 =str_replace("]","",$str1);
                    $newArr = explode(',',$str1);
                    $newArrs = array_merge($newArr,$newArrs);
                }
                if($newArrs){
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if($datas['type'] == 1){
                        if(!in_array('299',$newArrs)){
                            return $this->ajaxReturn(1,'无权查看');
                        }else{
                            $role['is_say'] = 1;
                        }
                        $role['is_modify'] = 0;
                        $role['is_check'] = 0;

                    }elseif($datas['type'] == 2){
                        if($datas['is_meeting'] == 1){
                            $banquet_end = $datas['end_year'] . '-' .$datas['end_month'] . '-' . $datas['end_day'];
                            if($datas['end_night'] == 1){
                                $banquet_end = strtotime($banquet_end . "+16 hours");
                            }else{
                                $banquet_end = strtotime($banquet_end . "+23 hours");
                            }
                        }else{
                            $banquet_end = $datas['begin_year'] . '-' .$datas['begin_month'] . '-' . $datas['begin_day'];
                            if($datas['begin_night'] == 1){
                                $banquet_end = strtotime($banquet_end . "+16 hours");
                            }else{
                                $banquet_end = strtotime($banquet_end . "+23 hours");
                            }
                        }
                        if($banquet_end){
                            if(time() < $banquet_end){
                                    // 结单全部
                                    if(!in_array('296',$newArrs)){
                                        // 修改
                                        if(!in_array('301',$newArrs)){
                                            // 查看
                                            if(!in_array('299',$newArrs)){
                                                return $this->ajaxReturn(1,'无权查看');
                                            }else{
                                                $role['is_say'] = 1;
                                                $role['is_modify'] = 0;
                                                $role['is_check'] = 0;
                                            }
                                        }else{
                                            $role['is_say'] = 0;
                                            $role['is_modify'] = 1;
                                            $role['is_check'] = 0;
                                        }
                                    }else{
                                        $role['is_say'] = 0;
                                        $role['is_modify'] = 1;
                                        $role['is_check'] = 0;
                                    }
                            }else{
                                $deposit = db::name('scheduled_deposit')->where('sid',$id)->where('status',1)->select();
                                $guarantee = db::name('guarantee_order')->where('did',$id)->where('status',1)->select();
                                $newOrder = array_merge($deposit,$guarantee);
                                $orderCount = count($newOrder);
                                if($orderCount){
                                    if(!in_array('296',$newArrs)){
                                        // 修改
                                        if(!in_array('301',$newArrs)){
                                            // 查看
                                            if(!in_array('299',$newArrs)){
                                                return $this->ajaxReturn(1,'无权查看');
                                            }else{
                                                $role['is_say'] = 1;
                                                $role['is_modify'] = 0;
                                                $role['is_check'] = 0;
                                            }
                                        }else{
                                            $role['is_say'] = 0;
                                            $role['is_modify'] = 1;
                                            $role['is_check'] = 0;
                                        }
                                    }else{
                                        $role['is_say'] = 0;
                                        $role['is_modify'] = 1;
                                        $role['is_check'] = 0;
                                    }
                                }else{
                                    if(!in_array('296',$newArrs)){
                                        // 修改
                                        if(!in_array('301',$newArrs)){
                                            // 查看
                                            if(!in_array('299',$newArrs)){
                                                return $this->ajaxReturn(1,'无权查看');
                                            }else{
                                                $role['is_say'] = 1;
                                                $role['is_modify'] = 0;
                                                $role['is_check'] = 0;
                                            }
                                        }else{
                                            $role['is_say'] = 0;
                                            $role['is_modify'] = 1;
                                            $role['is_check'] = 0;
                                        }
                                    }else{
                                        $role['is_say'] = 0;
                                        $role['is_modify'] = 0;
                                        $role['is_check'] = 1;
                                    }
                                }
                            }
                        }
                    }elseif($datas['type'] == 3){

                        if(!in_array('296',$newArrs)){
                            // 修改
                            if(!in_array('301',$newArrs)){
                                // 查看
                                if(!in_array('299',$newArrs)){
                                    return $this->ajaxReturn(1,'无权查看');
                                }else{
                                    $role['is_say'] = 1;
                                    $role['is_modify'] = 0;
                                    $role['is_check'] = 0;
                                }
                            }else{
                                $role['is_say'] = 1;
                                $role['is_modify'] = 0;
                                $role['is_check'] = 0;
                            }
                        }else{
                            $role['is_say'] = 1;
                            $role['is_modify'] = 0;
                            $role['is_check'] = 0;
                        }
                    }
                }
            }
        }

        if(!$datas){
            return $this->ajaxReturn(1,'暂无订单');
        }
        if($datas['other']){
            $image = explode( ",",$datas['other']);
            if(count($image) != 1){
                // $pic = [];
                foreach($image as $k=>$vs){
                    $pic[$k]['other_id'] = $vs;
                    $pic[$k]['others'] = get_file_path($vs);
                }
                $datas['others'] = $pic;
            }else{
                $pic = [];
                $pic['other_id'] = $datas['other'];
                $pic['others'] = get_file_path($datas['other']);
                $datas['others'][] = $pic;
            }
        }
        if($datas['contract']){
            $image = explode( ",",$datas['contract']);
            if(count($image) != 1){
                $pics = array();
                foreach($image as $ks=>$vs){
                    $pics[$ks]['contract_id'] = $vs;
                    $pics[$ks]['contracts'] = get_file_path($vs);
                }
                $datas['contracts'] = $pics;
            }else{
                $pics = array();
                $pics['contract_id'] = $datas['contract'];
                $pics['contracts'] = get_file_path($datas['contract']);
                $datas['contracts'][] = $pics;
            }
        }
        if($datas['is_meeting'] == 1){
            $datas['end_time'] = $datas['end_year'] . '-' . $datas['end_month']. '-' . $datas['end_day'];
        }
        $datas['source_id'] = $datas['source'];
        $datas['source'] = db::name('source')->where('id',$datas['source_id'])->value('name');
        
        $datas['begin_time'] = $datas['begin_year'] . '-' . $datas['begin_month']. '-' . $datas['begin_day'];
        unset($datas['begin_day']);
        unset($datas['begin_year']);
        unset($datas['begin_month']);
        $datas['addtime'] = date("Y-m-d H:i:s",$datas['addtime']);
        unset($datas['end_year']);
        unset($datas['end_month']);
        unset($datas['end_day']);
        unset($datas['end_night']);
        $where[] = ['tid','=',$datas['tid']];
        $where[] = ['hid','=',$user['hid']];
        $banquet_name = db::name('banquet_name')->where($where)
        ->field('id,name,names')->select();
        $newArr = [];
        foreach ($banquet_name as $k => $v) {
            $newArr[$k]['title'] = $v['name'];
            $names = db::name('banquet_names')->where('bid',$v['id'])->where('did',$id)->value('name');
            if($names){
                $names = $names;
            }else{
                $names = '';
            }
            $newArr[$k]['names'] = $names;
            $newArr[$k]['mobile'] = $v['names'];
            $namea = db::name('banquet_names')->where('bid',$v['id'])->where('did',$id)->value('value');
            if($names){
                $namea = $namea;
            }else{
                $namea = '';
            }
            $newArr[$k]['mobiles'] = $namea;
            $newArr[$k]['id'] = db::name('banquet_names')->where('bid',$v['id'])->where('did',$id)->value('id');
        }
        // 确认方式
        $list = db::name('scheduled_deposit')->where('sid',$id)->where('hid',$user['hid'])->
        field('id,num,image,type_id,pay_type,money,addtime,pay_name,type_name,status')->select();
        $guarantee = db::name('guarantee_order')->where('did',$id)->
        field('id,gid,addtime,status')->select();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['type'] = 1;
                if($v['image']){
                    $list[$k]['image_id'] = $v['image'];
                    $list[$k]['image'] = get_file_path($v['image']);
                }
            }
        }
        if($guarantee){
            foreach ($guarantee as $ks => $vs) {
                $guarantee[$ks]['type'] = 2;
                if($vs['gid']){
                    $guarantee[$ks]['g_name'] = db::name('guarantee')->where('id',$vs['gid'])->value('name');
                }
            }
        }
        $newArrsa = array_merge($list,$guarantee);
        $cmf_arr = array_column($newArrsa, 'addtime');
        array_multisort($cmf_arr, SORT_ASC, $newArrsa);


        if($datas['director_id'] != $user['id']){
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('301',$newArrs)){
                        return $this->ajaxReturn(1,'暂无权限');
                    }else{
                        // 查看姓名
                        if(!in_array('304',$newArrs)){
                           $datas['book'] = '***';
                        }
                        // 查看合同
                        if(!in_array('305',$newArrs)){
                            $datas['contracts'] = '';
                        }
                        // 查看备注
                        if(!in_array('306',$newArrs)){
                            $datas['content'] = '***';
                        }
                        // 查看手机号
                        if(!in_array('307',$newArrs)){
                            $list['mobile'] =  preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $list['mobile']);
                        }
                    }
                }
            }
        }


        $datas['increase_id'] =  db::name('int_increase')->where('sid',$datas['id'])->value('id');
        $data['scheduled_deposit'] = $newArrsa;
        $data['data'] = $datas;
        $data['newArr'] = $newArr;
        $data['role'] = $role;
        return $this->ajaxReturn(0,'成功',$data);
    }
     /**
     * @title 增值服务保存与修改
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/increase_save
     * @method POST
     * @param name:increase type:int require:1 default: other: desc:增值服务
     * @param name:id type:int require:0 default: other: desc:id（修改id）
     * @return cid:增值服务id
     * 
     **/
    public function increase_save()
    {
        $user = $this->user;
        // 增值订单
        $increase = input('increase');
        $id = input('id');
        $increase = json_decode($increase);
        if($increase){
            if($id){
                $did = db::name('int_increase')->where('id',$id)->value('did');
                $did = explode(',',$did);
                $count = count($did);
                if($count == 1){
                    db::name('increase_order')->where('id',$did[0])->delete();
                }else{
                    db::name('increase_order')->delete($did);
                }
                $oids = '';
                foreach ($increase as $k => $v) {
                    $res['oid'] = $v->id;
                    $res['money'] = $v->money;
                    $res['did'] = '';
                    $res['addtime'] = time();
                    $res['hid'] = $user['hid'];
                    $res['name'] = $v->name;
                    $res['num'] = $v->num;
                    $oid = db::name('increase_order')->insertGetid($res);
                    if($oid){
                        if(empty($oids)){
                            $oids = $oid;
                        }else{
                            $oids = $oids . ',' . $oid;
                        }
                    }
                }
                $re['did'] = $oids;
                if(db::name('int_increase')->where('id',$id)->update($re)){
                    $data['cid'] = $id;
                    return $this->ajaxReturn(0,'成功',$data);
                }
            }else{
                $oids = '';
                foreach ($increase as $k => $v) {
                    $res['oid'] = $v->id;
                    $res['money'] = $v->money;
                    $res['did'] = '';
                    $res['addtime'] = time();
                    $res['hid'] = $user['hid'];
                    $res['name'] = $v->name;
                    $res['num'] = $v->num;
                    $oid = db::name('increase_order')->insertGetid($res);
                    if($oid){
                        if(empty($oids)){
                            $oids = $oid;
                        }else{
                            $oids = $oids . ',' . $oid;
                        }
                    }
                }
                $re['did'] = $oids;
                $cid = db::name('int_increase')->insertGetid($re);
                if($cid){
                    $data['cid'] = $cid;
                    return $this->ajaxReturn(0,'成功',$data);
                }
            }
        }
        
    }
    /**
     * @title 获取已保存的增值服务
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/get_increase
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     **/
    public function get_increase()
    {
        $user = $this->user;
        // 增值服务
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }else{
            $res = db::name('int_increase')->where('id',$id)->value('did');
            $did = explode(',',$res);
            $count = count($did);
            $newArr = [];
            if($count == 1){
                $newArr[] = db::name('increase_order')->where('id',$res)->field('oid,money,num')->find();
            }else{
                for ($i=0; $i <$count; $i++) { 
                    $newArr[] = db::name('increase_order')->where('id',$did[$i])->field('oid,money,num')->find();
                }
            }
            $data['list'] = $newArr;
            return $this->ajaxReturn(0,'成功',$data);
        }
        
    }
    /**
     * @title 修改订单保存
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/order_save
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * 
     * 
     * @param name:book type:string require:1 default: other: desc:预订人
     * @param name:mobile type:int require:1 default: other: desc:预订人手机号
     * @param name:sex type:int require:1 default: other: desc:1男2女
     * 
     * 
     * @param name:desk type:int require:1 default: other: desc:桌数
     * @param name:prepare_desk type:int require:0 default: other: desc:备桌
     * @param name:did type:int require:0 default: other: desc:餐标id
     * @param name:d_money type:int require:0 default: other: desc:餐标金额
     * @param name:d_name type:string require:0 default: other: desc:餐标名称
     * @param name:cid type:string require:1 default: other: desc:所选菜品id
     * 
     * 
     * @param name:source type:int require:1 default: other: desc:客户来源id
     * @param name:director_id type:int require:1 default: other: desc:客户经理id
     * 
     * 
     * @param name:content type:string require:0 default: other: desc:备注
     * @param name:contract type:int require:0 default: other: desc:上传合同
     * @param name:other type:int require:0 default: other: desc:其他信息
     * 
     * 
     * @param name:confirm type:int require:0 default: other: desc:确认方式（type：1预付款2担保人 id：确认方式id若为新增可不传，num：编号，image：图片，type_id：类型id pay_type：支付方式，money：金额 guarantee：为担保人时只有担保人id）
     * 
     * 
     * @param name:banquet_name type:array require:0 default: other: desc:预留信息（bid修改bid，name：信息，value：手机号）
     * @param name:type type:int require:0 default: other: desc:1正常预定2发短信
     * @param name:increase_id type:int require:0 default: other: desc:增值服务id（若修改传参数若不修改参数为空）
     * 
     */
    public function order_save()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            $this->ajaxReturn(1,'缺少参数');
        }
        $scheduled = db::name('scheduled')->where('id',$id)->find();
        if(!$scheduled){
            $this->ajaxReturn(1,'订单异常');
        }
        if($scheduled['director_id'] == $user['id']){


        }else{

        }
        $did = input('did');
        $d_name = input('d_name');
        // 预留信息
        $banquet_name = input('banquet_name');
        if($banquet_name){
            $banquet_name = json_decode($banquet_name);
            foreach ($banquet_name as $k => $v) {
                if(property_exists($v,'bid')){
                    $banquet = db::name('banquet_names')->where('id',$v->bid)->update(['name'=>$v->name,'value'=>$v->value]);
                }else{
                    $this->ajaxReturn(1,'缺少参数');
                }
            }
        }else{
            $banquet = false;
        }
        // 预订人
        $data['book'] = input('book');
        $data['mobile'] = input('mobile');
        $data['sex'] = input('sex');
        $bid = db::name('scheduled')->where('id',$id)->value('bid');
        // 桌数备桌
        $data['desk'] = input('desk');
        $data['prepare_desk'] = input('prepare_desk');
        if(!$did){
            $data['did'] = 0;
            $data['d_money'] = input('d_money');
            $data['d_name'] = $d_name;
            if($data['d_money']){
                // 宴会厅的最低金额
                $ba_money = db::name('banquet')->where('id',$bid)->value('money');
                if($ba_money){
                    // 判断输入金额与后台设定最低金额
                    if($data['d_money'] < $ba_money){
                        return $this->ajaxReturn(1,'餐标金额小于最低金额');
                    }
                }
            }
        }else{
            $data['did'] = input('did');
            $data['d_money'] = db::name('ds')->where('id',$did)->value('money');
            $data['d_name'] = db::name('ds')->where('id',$did)->value('name');
        }
        $data['content'] = input('content');
        $data['contract'] = input('contract');
        $data['other'] = input('other');
        $data['source'] = input('source');
        $data['director_id'] = input('director_id');
        if($data['director_id']){
            $data['director'] = db::name('user')->where('id',$data['director_id'])->value('username');
        }
        $data['modifytime'] = time();
        $confirm = input('confirm');


        if(isset($confirm)){
            if(!empty($confirm)){
                $confirm = json_decode(stripslashes(html_entity_decode($confirm)),true);
                if($confirm){
                    foreach ($confirm as $k => $v) {
                        if(isset($v['id'])){
                            // 预收款
                            $types = $v['type'];
                            if($types == 1){
                                if(isset($v['num'])){
                                    $up['num'] = $v['num'];
                                }else{
                                    $this->ajaxReturn(1,'请补充编号');
                                }
                                if(isset($v['image'])){
                                    $up['image'] = $v['image'];
                                }else{
                                    $this->ajaxReturn(1,'请上传收据');
                                }
                                if(isset($v['type_id'])){
                                    $up['type_id'] = $v['type_id'];
                                    $up['type_name'] = db::name('deposit')->where('id',$up['type_id'])->value('name');
                                }else{
                                    $this->ajaxReturn(1,'请选择支付类型');
                                }
                                if(isset($v['pay_type'])){
                                    $up['pay_type'] = $v['pay_type'];
                                    $up['pay_name'] = db::name('deposit')->where('id',$up['pay_type'])->value('name');
                                }else{
                                    $this->ajaxReturn(1,'请选择支付方式');
                                }
                                if(isset($v['money'])){
                                    $up['money'] = $v['money'];
                                }else{
                                    $this->ajaxReturn(1,'请填写金额');
                                }
                                $confirms = db::name('scheduled_deposit')->where('id',$v['id'])->update($up);
                                // 担保人
                            }elseif($types == 2){
                                $up['guarantee'] = $v['guarantee'];
                                $confirms = db::name('scheduled_deposit')->where('id',$v['id'])->update($up);
                            }
                        }else{

                            $types = $v['type'];
                            if($types == 1){
                                $res['sid'] = $id;
                                $res['hid'] = $user['hid'];
                                if(isset($v['num'])){
                                    $res['num'] = $v['num'];
                                }else{
                                    $this->ajaxReturn(1,'请补充编号');
                                }
                                if(isset($v['image'])){
                                    $res['image'] = $v['image'];
                                }else{
                                    $this->ajaxReturn(1,'请上传收据');
                                }
                                if(isset($v['type_id'])){
                                    $res['type_id'] = $v['type_id'];
                                    $res['type_name'] = db::name('deposit')->where('id',$res['type_id'])->value('name');
                                }else{
                                    $this->ajaxReturn(1,'请选择支付类型');
                                }
                                if(isset($v['pay_type'])){
                                    $res['pay_type'] = $v['pay_type'];
                                    $res['pay_name'] = db::name('deposit')->where('id',$res['pay_type'])->value('name');
                                }else{
                                    $this->ajaxReturn(1,'请选择支付方式');
                                }
                                if(isset($v['money'])){
                                    $res['money'] = $v['money'];
                                }else{
                                    $this->ajaxReturn(1,'请填写金额');
                                }
                                $res['addtime'] = time();
                                $res['manager_id'] = $user['id'];
                                $confirms = db::name('scheduled_deposit')->insert($res);
                                if($confirms){
                                    db::name('scheduled')->where('id',$id)->update(['type'=>2]);
                                    db::name('scheduled')->where('id',$id)->update(['status'=>1]);
                                    db::name('scheduled')->where('id',$id)->update(['paytime'=>time()]);
                                }
                                // 担保人
                            }elseif($types == 2){
                                $res['gid'] = $v['guarantee'];
                                $res['did'] = $id;
                                $res['addtime'] = time();
                                $confirms = db::name('guarantee_order')->insert($res);
                                if($confirms){
                                    db::name('scheduled')->where('id',$id)->update(['type'=>2]);
                                    db::name('scheduled')->where('id',$id)->update(['status'=>1]);
                                    db::name('scheduled')->where('id',$id)->update(['paytime'=>time()]);
    
                                }
                            }
                        }
                   
                    }
                }
              
            }
        }else{
            $confirms = false;
        }
        
        $increase_id = input('increase_id');
        if(!empty($increase_id)){
            // print_r($increase_id);die;
            db::name('int_increase')->where('id',$increase_id)->update(['sid'=>$id]);
            $didsa = db::name('int_increase')->where('id',$increase_id)->value('did');

            $dids = explode(',',$didsa);
            $count = count($dids);
            if($count == 1){
                // print_r($id);die;
                db::name('increase_order')->where('id',$didsa)->update(['did'=>$id]);

                $increase_order = db::name('increase_order')->where('id',$didsa)->find();
                $money = $increase_order['num'] * $increase_order['money'];
                
                db::name('scheduled')->where('id',$id)->update(['increase_money'=>$money]);
            }else{
                $money = 0;
                for ($i=0; $i <$count ; $i++) { 
                    db::name('increase_order')->where('id',$dids[$i])->update(['did'=>$id]);
                    $increase_order = db::name('increase_order')->where('id',$didsa)->find();
                    $moneys = $increase_order['num'] * $increase_order['money'];
                    
                    $money = $money + $moneys;
                }
                db::name('scheduled')->where('id',$id)->update(['increase_money'=>$money]);
            }
        }
        $type = input('type');
        if($type == 2){
            // 预订人
            $userd = input('book');
            // 预定时间
            $date =  $scheduled['begin_year'] . '-' . $scheduled['begin_month'] . '-' . $scheduled['begin_day'];
            // 宴会厅
            $hall = $scheduled['b_name'];
            // 客户经理
            $staff = $data['director'];
            // 客户经理手机号
            $phone = db::name('user')->where('id',$data['director_id'])->value('mobiles');
            $mobile =  input('mobile');
            // $phone = '19939298752';
            $msg = ext_send_alismsa($mobile,$userd,$date,$hall,$staff,$phone);
            if($msg['code'] == 'error'){
                return $this->ajaxReturn(1,$msg['msg']);
            }
        }
        if(db::name('scheduled')->where('id',$id)->update($data)){
            return $this->ajaxReturn(0,'成功');
        }else{
            if($confirms){
                return $this->ajaxReturn(0,'成功');

            }elseif($banquet){
                return $this->ajaxReturn(0,'成功');

            }elseif($increase_id){
                return $this->ajaxReturn(0,'成功');
            }else{
                return $this->ajaxReturn(1,'失败');
            }
        }


    }
    /**
     * @title 预定下单
     * @description 接口说明
     * @author 开发者
     * @url /api/Scheduled/scheduled
     * @method POST
     * @param name:bid type:int require:1 default: other: desc:宴会厅id
     * @param name:tid type:int require:1 default: other: desc:类型id
     * @param name:begin_year type:int require:1 default: other: desc:开始年份
     * @param name:begin_month type:int require:1 default: other: desc:开始月份
     * @param name:begin_day type:int require:1 default: other: desc:开始天数
     * @param name:begin_night type:int require:1 default: other: desc:1中午2晚上
     * @param name:end_year type:int require:0 default: other: desc:结束年份
     * @param name:end_month type:int require:0 default: other: desc:结束月份
     * @param name:end_day type:int require:0 default: other: desc:结束天数
     * @param name:end_night type:int require:0 default: other: desc:1中午2晚上
     * @param name:book type:int require:1 default: other: desc:预订人
     * @param name:mobile type:int require:1 default: other: desc:预订人手机号
     * @param name:sex type:int require:1 default: other: desc:1男2女
     * @param name:desk type:int require:1 default: other: desc:桌数
     * @param name:prepare_desk type:int require:0 default: other: desc:备桌
     * @param name:did type:int require:0 default: other: desc:餐标id
     * @param name:d_money type:int require:0 default: other: desc:餐标金额
     * @param name:d_name type:int require:0 default: other: desc:餐标名称
     * @param name:cid type:int require:1 default: other: desc:所选菜品id
     * @param name:increase_money type:int require:0 default: other: desc:增值服务费
     * @param name:source type:int require:1 default: other: desc:客户来源id
     * @param name:content type:int require:0 default: other: desc:备注
     * @param name:contract type:int require:0 default: other: desc:上传合同
     * @param name:other type:int require:0 default: other: desc:其他信息
     * @param name:is_meeting type:int require:1 default: other: desc:是否会议1是2不是
     * @param name:banquet_name type:int require:1 default: other: desc:新娘新郎信息等
     * @param name:increase type:int require:1 default: other: desc:增值服务id
     * @param name:confirm type:int require:0 default: other: desc:确认方式（数组）
     * 
     * @param name:director_id type:int require:0 default: other: desc:客户经理id
     * @param name:type type:int require:0 default: other: desc:1正常预定2发短信
     * 
     * 
     */
    public function scheduled()
    {
        $user = $this->user;


        $bid = input('bid');
        $did = input('did');
        $cid = input('cid');
        $is_meeting = input('is_meeting');
        $d_name = input('d_name');
        $confirm = input('confirm');
        $increase = input('increase');
        $hid = $user['hid'];
        // $director_id = input('director_id');
        
        $begin_year = input('begin_year');
        $begin_month = input('begin_month');
        $begin_day = input('begin_day');
        $begin_night = input('begin_night');

        $end_year = input('end_year');
        $end_month = input('end_month');
        $end_day = input('end_day');
        $end_night = input('end_night');
        $begin = $begin_year .'-'.$begin_month . '-' . $begin_day;
        $start = strtotime($begin);
        $end = $end_year .'-'.$end_month . '-' . $end_day;
        $end = strtotime($end);
        // $tid = input('tid');
        if($is_meeting == 1){
            $data['end_year'] = input('end_year');
            $data['end_month'] = input('end_month');
            $data['end_day'] = input('end_day');
            $data['end_night'] = input('end_night');//1,2

                $map[] = ['is_meeting','=',1];
                $map[] = ['hid','=',$hid];
                $map[] = ['type','=',2];
                $map[] = ['bid','=',$bid];
                $map[] = ['is_del','=',2];
                $map[] = ['begin_year','=',$begin_year];
                $order = db::name('scheduled')->where($map)->select();
                if($order){
                    foreach ($order as $ok => $ov) {
                        $start1 = $ov['begin_year'] . $ov['begin_month'] . $ov['begin_day'];
                        $end1 = $ov['end_year'] . $ov['end_month'] . $ov['end_day'];

                        $start1 = strtotime($start1);
                        $end1 = strtotime($end1);
                        
                        $end_nights = $ov['end_night'];
                        $begin_nights = $ov['begin_night'];//1上午，2下午

                        // $start1   $end1
                        // $start    $end
                        if($start > $end1){
                            // return true;
                        }elseif($start == $end1){
                            if($begin_night > $end_nights){
                                // return true;
                            }else{
                                return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                            }
                        }elseif($start < $end1){
                            if($start < $start1){
                                if($end < $start1){
                                    // return true;
                                }elseif($end == $start1){
                                    if($end_night < $begin_nights){
                                        // return true;
                                    }else{
                                        return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                                    }
                                }elseif($end > $start1){
                                    return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                                }
                            }elseif($start == $start1){
                                if($begin_night < $begin_nights){
                                    if($end == $end1 && $end_night < $end_nights){
                                        // return true;
                                    }else{
                                        return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                                    }
                                }else{
                                    return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                                }
                            }else{
                                return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                            }
                        }     

                        //20210507   20210702  
                        // if($end1 <= $start){

                        //     // if($end_nights == 2){
                        //     //         return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        //     // }elseif($end_nights == 1){
                        //     //     if($begin_night == 1){
                        //     //         return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        //     //     }
                        //     // }
                        // }elseif($end1 > $start && $end1 < $end){
                        //     return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        // }elseif($end1 > $end){
                        //     if($start1 < $start){
                        //         return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        //     }elseif($start1 > $start && $start1 < $end){
                        //         return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        //     }elseif($start1 >= $end){
                        //         if($begin_nights == 1){
                        //             return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间'.$ov['id']);
                        //         }elseif($begin_nights == 2){
                        //             if($begin_night == 2){
                        //                 return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        //             }
                        //         }
                        //     }
                        // }
                    }
                }
         
                $maps[] = ['is_meeting','=',2];
                $maps[] = ['hid','=',$hid];
                $maps[] = ['begin_month','between',[$begin_month,$end_month]];
                $maps[] = ['begin_day','between',[$begin_day,$end_day]];
                $maps[] = ['type','=',2];
                $maps[] = ['bid','=',$bid];
                $maps[] = ['is_del','=',2];
                $orders = db::name('scheduled')->where($maps)->select();
                if($orders){
                    return $this->ajaxReturn(1,'此时间段已有预定订单');
                }

                
               
        }elseif($is_meeting == 2){
            $is_meeting = 2;
            $map[] = ['is_meeting','=',2];
            $map[] = ['begin_year','=',$begin_year];
            $map[] = ['begin_month','=',$begin_month];
            $map[] = ['begin_day','=',$begin_day];
            $map[] = ['begin_night','=',$begin_night];
            $map[] = ['type','=',2];
            $map[] = ['is_del','=',2];
            $map[] = ['hid','=',$hid];
            $map[] = ['bid','=',$bid];

            $order = db::name('scheduled')->where($map)->count();
            if($order){
                return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
            }else{
                // 会议订单
                $maps[] = ['is_meeting','=',1];
                $maps[] = ['hid','=',$hid];
                $maps[] = ['type','=',2];
                $maps[] = ['bid','=',$bid];
                $maps[] = ['is_del','=',2];
                $maps[] = ['begin_year','=',$begin_year];
                $orders = db::name('scheduled')->where($maps)->select();
                foreach ($orders as $ok => $ov) {
                    $start1 = $ov['begin_year'] . $ov['begin_month'] . $ov['begin_day'];
                    $end1 = $ov['end_year'] . $ov['end_month'] . $ov['end_day'];
                    $start1 = strtotime($start1);
                    $end1 = strtotime($end1);


                    $end_nights = $ov['end_night'];
                    $begin_nights = $ov['begin_night'];
                    // 1在会议外可预定
                    // 2在会议内不可预定
                    if($begin > $start1 && $begin < $end1){
                         return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                    }elseif($begin == $start1){
                        if($begin_nights == 1){
                            return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        }elseif($begin_nights == 2 && $begin_night == 2){
                            return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        }
                    }elseif($begin == $end1){
                        if($end_nights == 2){
                            return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        }elseif($begin_nights == 1 && $begin_night == 1){
                            return $this->ajaxReturn(1,'此宴会厅已有相同的预定时间');
                        }
                    }
                    
                }
            }
        }


        if(!$did){
            $data['did'] = 0;
            $data['d_money'] = input('d_money');
            $data['d_name'] = $d_name;
            if($data['d_money']){
                // 宴会厅的最低金额
                $ba_money = db::name('banquet')->where('id',$bid)->value('money');
                if($ba_money){
                    // 判断输入金额与后台设定最低金额
                    if($data['d_money'] < $ba_money){
                        return $this->ajaxReturn(1,'餐标金额小于最低金额');
                    }
                }
            }
        }else{

            $data['did'] = input('did');
            $data['d_money'] = db::name('ds')->where('id',$did)->value('money');
            $data['d_name'] = db::name('ds')->where('id',$did)->value('name');
        }
        $data['bid'] = $bid;
        $data['b_name'] = db::name('banquet')->where('id',$bid)->value('name');
        $data['tid'] = input('tid');
        $data['t_name'] = db::name('banquet_types')->where('id',$data['tid'])->value('name');
        $data['begin_year'] = input('begin_year');
        $data['begin_month'] = input('begin_month');
        $data['begin_day'] = input('begin_day');
        $data['begin_night'] = input('begin_night');
        $data['book'] = input('book');
        $data['mobile'] = input('mobile');

        $data['director_id'] = input('director_id');

        $data['sex'] = input('sex');
        $data['desk'] = input('desk');
        $data['prepare_desk'] = input('prepare_desk');
        $data['source'] = input('source');
        $data['director'] = db::name('user')->where('id', $data['director_id'])->value('username');
        $data['content'] = input('content');
        $data['contract'] = input('contract');
        $data['other'] = input('other');
        $data['addtime'] = time();
        $data['hid'] = $user['hid'];
        $data['is_meeting'] = $is_meeting;
        $data['increase_money'] = input('increase_money');
        $data['cid'] = $cid;
        // print_r($data);die;
        if(input('mobile')){
            $mobile = input('mobile');
            $users = db::name('client')->where('mobile',$mobile)->where('hid',$user['hid'])->find();
            if(!$users){
                $use['name'] = input('book');
                $use['mobile'] = input('mobile');
                $use['hid'] = $user['hid'];
                $use['source_id'] = input('source');
                $use['manager_id'] = $user['id'];
                $use['addtime'] = time();
                $use['manager'] = db::name('user')->where('id',$user['id'])->value('username');
                $uid = db::name('client')->insertGetId($use);
                $data['uid'] = $uid;
            }else{
                $data['uid'] = $users['id'];
            }
        }

        $type = input('type');

        if($type == 2){
            // 预订人
            $userd = input('book');
            $date =  $data['begin_year'] . '-' . $data['begin_month'] . '-' . $data['begin_day'];
            $hall = $data['b_name'];
            $staff = $data['director'];
            $phone = db::name('user')->where('id', $data['director_id'])->value('mobiles');
            $mobile =  input('mobile');
            // $phone = '199';
            $msg = ext_send_alismsa($mobile,$userd,$date,$hall,$staff,$phone);
            if($msg['code'] == 'error'){
                return $this->ajaxReturn(1,$msg['msg']);
            }
        }
        $oid = db::name('scheduled')->insertGetId($data);
        // $oid = 1;
        if($increase){
            db::name('int_increase')->where('id',$increase)->update(['sid'=>$oid]);
            $didsa = db::name('int_increase')->where('id',$increase)->value('did');

            $dids = explode(',',$didsa);
            $count = count($dids);
            if($count == 1){
                db::name('increase_order')->where('id',$didsa)->update(['did'=>$oid]);
                $increase_order = db::name('increase_order')->where('id',$didsa)->find();
                $money = $increase_order['num'] * $increase_order['money'];
                db::name('scheduled')->where('id',$oid)->update(['increase_money'=>$money]);
            }else{
                $moneys = 0;
                for ($i=0; $i <$count ; $i++) { 
                    db::name('increase_order')->where('id',$dids[$i])->update(['did'=>$oid]);

                    $increase_order = db::name('increase_order')->where('id',$dids[$i])->find();
                    $money = $increase_order['num'] * $increase_order['money'];
                    // print_R($money);die;
                    $moneys = $moneys + $money;
                }
                db::name('scheduled')->where('id',$oid)->update(['increase_money'=>$moneys]);
            }
        }else{
            db::name('scheduled')->where('id',$oid)->update(['increase_money'=>0]);
        }
        if(!empty($confirm)){
            $confirm = json_decode(stripslashes(html_entity_decode($confirm)),true);
            foreach ($confirm as $k => $v) {
                // 预收款
                $types = $v['type'];

                if($types == 1){
                    $res['sid'] = $oid;
                    $res['hid'] = $user['hid'];
                    if(isset($v['num'])){
                        $res['num'] = $v['num'];
                    }else{
                        $this->ajaxReturn(1,'请补充编号');
                    }
                    if(isset($v['image'])){
                        $res['image'] = $v['image'];
                    }else{
                        $this->ajaxReturn(1,'请上传收据');
                    }
                    if(isset($v['type_id'])){
                        $res['type_id'] = $v['type_id'];
                        $res['type_name'] = db::name('deposit')->where('id',$res['type_id'])->value('name');
                    }else{
                        $this->ajaxReturn(1,'请选择支付类型');
                    }
                    if(isset($v['pay_type'])){
                        $res['pay_type'] = $v['pay_type'];
                        $res['pay_name'] = db::name('deposit')->where('id',$res['pay_type'])->value('name');
                    }else{
                        $this->ajaxReturn(1,'请选择支付方式');
                    }
                    if(isset($v['money'])){
                        $res['money'] = $v['money'];
                    }else{
                        $this->ajaxReturn(1,'请填写金额');
                    }
                    $res['addtime'] = time();
                    $res['manager_id'] = $user['id'];
                    $confirms = db::name('scheduled_deposit')->insert($res);
                    if($confirms){
                        db::name('scheduled')->where('id',$oid)->update(['type'=>2]);
                        db::name('scheduled')->where('id',$oid)->update(['status'=>1]);
                        db::name('scheduled')->where('id',$oid)->update(['paytime'=>time()]);
                    }
                    // 担保人
                }elseif($types == 2){
                    $res['gid'] = $v['guarantee'];
                    $res['did'] = $oid;
                    $res['addtime'] = time();
                    $confirms = db::name('guarantee_order')->insert($res);
                    if($confirms){
                        db::name('scheduled')->where('id',$res['did'])->update(['type'=>2]);
                        db::name('scheduled')->where('id',$res['did'])->update(['paytime'=>time()]);
                        db::name('scheduled')->where('id',$res['did'])->update(['status'=>1]);

                    }
                }
            }
        }
        // 预留手机电话
        $banquet_name = input('banquet_name');
        if($banquet_name){
            $banquet_name = json_decode($banquet_name);
            foreach ($banquet_name as $k => $v) {
                $rea['bid'] = $v->bid;
                $rea['name'] = $v->name;
                $rea['value'] = $v->value;
                $rea['hid'] = $user['hid'];
                $rea['did'] = $oid;
                db::name('banquet_names')->insert($rea);
            }
        }
        if(!empty($confirm)){
            return $this->ajaxReturn(0,'预定成功');

        }else{
            return $this->ajaxReturn(0,'预留成功');

        }
    }
}